package FlinkTest.day06;

import com.atguigu.datastream.bean.WaterSensor;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.tuple.Tuple;
import org.apache.flink.streaming.api.datastream.*;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;
import org.apache.flink.util.OutputTag;

import java.time.Duration;

/**
 * ClassName: Flink03_WaterMark_AllowedLateness_SideOutput
 * Package: FlinkTest.day06
 * Description:
 *             延迟数据侧输出
 *
 * @Author ChenJun
 * @Create 2023/4/13 15:30
 * @Version 1.0
 */
public class Flink03_WaterMark_AllowedLateness_SideOutput {
    public static void main(String[] args) throws Exception {

        //1. 创建流的运行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(1);

        //2. 从端口读取数据
        DataStreamSource<String> streamSource = env.socketTextStream("localhost", 9999);

        //3. 将数据转换为JavaBean对象
        SingleOutputStreamOperator<WaterSensor> map = streamSource.map(new MapFunction<String, WaterSensor>() {
            @Override
            public WaterSensor map(String value) throws Exception {
                String[] split = value.split(",");
                return new WaterSensor(split[0], Long.parseLong(split[1]), Integer.parseInt(split[2]));
            }
        });

        //4 TODO 指定WaterMark以及事件时间戳   使用乱序流中的WaterMark 乱序程度为3s
        SingleOutputStreamOperator<WaterSensor> outputStreamOperator =
                map.assignTimestampsAndWatermarks(WatermarkStrategy.<WaterSensor>forBoundedOutOfOrderness(Duration.ofSeconds(3)).withTimestampAssigner(new SerializableTimestampAssigner<WaterSensor>() {
                    @Override
                    public long extractTimestamp(WaterSensor element, long recordTimestamp) {
                        return element.getTs() * 1000;
                    }
                }));

        //5. 将相同的key聚合到一起
        KeyedStream<WaterSensor, Tuple> keyedStream = outputStreamOperator.keyBy("id");

        // TODO 6.开启基于事件时间的窗口 大小为5s
        // TODO 定义一个侧输出流  注意！！！！！一定得是匿名内部类  花括号一定不要丢
        OutputTag<WaterSensor> outputTag = new OutputTag<WaterSensor>("outputTag") {
        };

        WindowedStream<WaterSensor, Tuple, TimeWindow> window = keyedStream.window(TumblingEventTimeWindows.of(Time.seconds(5)))
                .allowedLateness(Time.seconds(4))
                .sideOutputLateData(outputTag);

        SingleOutputStreamOperator<String> process = window.process(new ProcessWindowFunction<WaterSensor, String, Tuple, TimeWindow>() {
            @Override
            public void process(Tuple tuple, ProcessWindowFunction<WaterSensor, String, Tuple, TimeWindow>.Context context, Iterable<WaterSensor> elements, Collector<String> out) throws Exception {
                String msg =
                        "窗口: [" + context.window().getStart() / 1000 + "," + context.window().getEnd() / 1000 + ") 一共有 "
                                + elements.spliterator().estimateSize() + "条数据 ";
                out.collect(msg);
            }
        });

        SingleOutputStreamOperator<WaterSensor> vc = window.sum("vc");
        vc.print();
        process.print();

        // TODO 获取侧输出流
        DataStream<WaterSensor> sideOutput = vc.getSideOutput(outputTag);

        SingleOutputStreamOperator<String> outputresult = sideOutput.map(new MapFunction<WaterSensor, String>() {
            @Override
            public String map(WaterSensor value) throws Exception {
                return value + "下次别迟到！！！";
            }
        });
        outputresult.print("侧输出：");


        env.execute();

    }
}
